package com.rongji.dfish.demo.pub.view;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.rongji.dfish.base.Utils;
import com.rongji.dfish.ui.Combo;
import com.rongji.dfish.ui.Highlight;
import com.rongji.dfish.ui.Scrollable;
import com.rongji.dfish.ui.Widget;
import com.rongji.dfish.ui.command.AjaxCommand;
import com.rongji.dfish.ui.command.AlertCommand;
import com.rongji.dfish.ui.command.DialogCommand;
import com.rongji.dfish.ui.command.LoadingCommand;
import com.rongji.dfish.ui.command.SubmitCommand;
import com.rongji.dfish.ui.helper.FormPanel;
import com.rongji.dfish.ui.helper.GridLayoutFormPanel;
import com.rongji.dfish.ui.helper.GridPanel;
import com.rongji.dfish.ui.layout.ButtonBar;
import com.rongji.dfish.ui.layout.HorizontalLayout;
import com.rongji.dfish.ui.layout.VerticalLayout;
import com.rongji.dfish.ui.layout.View;
import com.rongji.dfish.ui.layout.grid.GridColumn;
import com.rongji.dfish.ui.layout.grid.Tr;
import com.rongji.dfish.ui.widget.Button;
import com.rongji.dfish.ui.widget.DialogTemplate;
import com.rongji.dfish.ui.widget.Html;
import com.rongji.dfish.ui.widget.PageBar;
import com.rongji.dfish.ui.widget.Split;
import com.rongji.dfish.ui.widget.TemplateTitle;
import com.rongji.dfish.ui.widget.TemplateView;
import com.rongji.dfish.ui.widget.TreePanel;

public class PubView {

	public static DialogTemplate getStdTemplate() {
		DialogTemplate std=new DialogTemplate().setCls("dlg-std").setId("std");
	
		VerticalLayout vp=new VerticalLayout(null);
		std.setNode(vp);
		HorizontalLayout head= new HorizontalLayout(null).setCls("dlg-std-head");
		vp.add(head,"40");
		head.add(new TemplateTitle(null).setCls("dlg-std-title").setWmin(10),"*");
		head.add(new Html(null,"<i class=ico-dlg-max></i>").setCls("dlg-max").setAlign("center").setValign("middle").setOn(Html.EVENT_CLICK, "dfish.dialog(this).max(this)"),"40");
		head.add(new Html(null,"<i class=ico-dlg-close></i>").setCls("dlg-close").setAlign("center").setValign("middle").setOn(Html.EVENT_CLICK, "dfish.dialog(this).close()"),"40");
		
		TemplateView cont=new TemplateView(null);
		vp.add(cont,"*");
		return std;
	}
	
	public static DialogTemplate getAlertTemplate() {
		DialogTemplate std=new DialogTemplate().setCls("dlg-std").setId("alert");
	
		VerticalLayout vp=new VerticalLayout(null);
		std.setNode(vp);
		HorizontalLayout head= new HorizontalLayout(null).setCls("dlg-std-head");
		vp.add(head,"40");
		head.add(new TemplateTitle(null).setCls("dlg-std-title").setWmin(10),"*");
		head.add(new Html(null,"<i class=ico-dlg-close></i>").setCls("dlg-close").setAlign("center").setValign("middle").setOn(Html.EVENT_CLICK, "dfish.dialog(this).close()"),"40");
		
		TemplateView cont=new TemplateView(null);
		vp.add(cont,"*");
		return std;
	}
	
	public static DialogTemplate getProngTemplate() {
		DialogTemplate std=new DialogTemplate().setCls("dlg-prong").setIndent(-10).setId("prong");
		VerticalLayout vp=new VerticalLayout(null);
		std.setNode(vp);
		vp.setAftercontent("<div class=dlg-prong-arrow-out></div><div class=dlg-prong-arrow-in></div>");
		
		HorizontalLayout head= new HorizontalLayout(null).setCls("dlg-prong-head");
		vp.add(head,"36");
		head.add(new TemplateTitle(null).setCls("dlg-prong-title"),"*");
		head.add(new Html(null,"<i onclick=dfish.close(this) class='dlg-close'>&times;</i><i class=f-vi></i>")
				.setAlign(Html.ALIGN_CENTER)
				.setValign(Html.VALIGN_MIDDLE),
				"40");
		
		TemplateView cont=new TemplateView(null);
		vp.add(cont,"*");
		return std;
	}
	
	/**
	 * 消息提示框
	 * @param alertInfo 消息内容
	 * @return
	 */
	public static AlertCommand getAlertInfo(String alertInfo) {
		AlertCommand alert = new AlertCommand(alertInfo).setPosition(AlertCommand.POSITION_BOTTOMRIGHT).setTimeout(5);
		return alert;
	}
	/**
	 * 警告提示框
	 * @param alertInfo
	 * @return
	 */
	public static AlertCommand getAlertWarn(String alertInfo) {
		AlertCommand alert = new AlertCommand(alertInfo).setCover(true);
		return alert;
	}
	
	public static final String BODY_ROOT = "b_root";
	public static final String BODY_MENU = "b_menu";
	public static final String BODY_SPLIT = "b_split";
	public static final String MAIN_ROOT = "m_root";
	public static final String MAIN_TOP = "m_top";
	public static final String MAIN_TITLE_SHELL = "m_title_shell";
	public static final String MAIN_TITLE = "m_title";
	public static final String MAIN_OPER_SHELL = "m_oper_shell";
	public static final String MAIN_OPER = "m_oper";
	public static final String MAIN_DISPLAY = "m_display";
	
	
	/**
	 * 构建
	 * @return
	 */
	public static View buildBodyView() {
		View view = new View();
		
		HorizontalLayout root = new HorizontalLayout(BODY_ROOT);
		view.add(root);
		root.setCls("bg-body");
		
		TreePanel menu = new TreePanel(BODY_MENU).setScroll(true).setCls("bg-menu bd-menu bd-onlyright").setWmin(1);
		root.add(menu, "200");
		
		Split split = new Split().setId(BODY_SPLIT);
//		split.setRange("0,230").setIcon(".ico-split").setOpenicon(".ico-split-open");
		root.add(split, "10");
		
		Widget<?> main = buildMainLayout();
		root.add(main, "*");
		
		return view;
	}
	
	public static VerticalLayout buildMainLayout() {
		VerticalLayout mRoot = new VerticalLayout(MAIN_ROOT);
		mRoot.setCls("bg-mod bd-mod").setStyle("margin:10px 10px 10px 0;").setWmin(12).setHmin(22);
		
		HorizontalLayout mTop = new HorizontalLayout(MAIN_TOP);
		mRoot.add(mTop, "50");
		mTop.setCls("bg-white bd-split bd-onlybottom").setHmin(1);
		
		VerticalLayout mTitleShell = new VerticalLayout(MAIN_TITLE_SHELL);
		mTop.add(mTitleShell);
		mTitleShell.setStyle("margin-left:20px;").setWmin(20);
		
		Html mTitle = new Html(null).setId(MAIN_TITLE).setValign(Html.VALIGN_MIDDLE).setCls("f-14");
		mTitleShell.add(mTitle);
		
		VerticalLayout mOperShell = new VerticalLayout(MAIN_OPER_SHELL);
		mTop.add(mOperShell, "350");
		mOperShell.setStyle("margin-right:20px;").setWmin(20);
		
		ButtonBar mOper = new ButtonBar(MAIN_OPER);
		mOperShell.add(mOper);
		mOper.setAlign(ButtonBar.ALIGN_RIGHT).setSpace(8);
		mOper.setPub(new Button().setCls("btn"));
		
		VerticalLayout mDisplay = new VerticalLayout(MAIN_DISPLAY);
		mRoot.add(mDisplay);
		mDisplay.setCls("bg-white");
		
		return mRoot;
	}
	
	public static View buildDialogView(boolean complex) {
		Widget<?> main = null;
		if (complex) {
			main = new GridLayoutFormPanel(null).setScroll(true);
		} else {
			main = new FormPanel(null).setScroll(true);
		}
		return buildDialogView(main);
	}
	
	public static View buildDialogView() {
		return buildDialogView(false);
	}
	
	public static final String DIALOG_ROOT = "dlg_root";
	public static final String DIALOG_MAIN = "dlg_main";
	public static final String DIALOG_BTN = "dlg_btn";
	
	public static View buildDialogView(Widget<?> main) {
		View view = new View();
		
		VerticalLayout root = new VerticalLayout(DIALOG_ROOT);
		view.add(root);
		root.setCls("bg-white bd-dlg").setWmin(2).setHmin(2);;
		
		if (main == null) {
			main = new VerticalLayout(DIALOG_MAIN);
		} else {
			main.setId(DIALOG_MAIN);
		}
		root.add(main);
		if (main instanceof Scrollable) { // 默认支持滚动
			((Scrollable<?>) main).setScroll(true);
		}
		main.setCls("bg-white").setStyle("padding:10px 20px;").setHmin(20).setWmin(40);
		
		ButtonBar btnBar = new ButtonBar(DIALOG_BTN);
		root.add(btnBar, "50");
		btnBar.setSpace(8).setAlign(ButtonBar.ALIGN_RIGHT).setCls("bg-dlg-bottom").setStyle("padding-right:20px;").setWmin(20);
		btnBar.getPub().setCls("btn");
		
		btnBar.add(new Button("取消").setOn(Button.EVENT_CLICK, "dfish.close(this);"));
		
		return view;
	}
	
	public static AjaxCommand getAjaxCommand(String src) {
		return getAjaxCommand(src, null);
	}
	
	public static AjaxCommand getAjaxCommand(String src, Boolean cover) {
		return getAjaxCommand(src, cover, null);
	}
	
	public static AjaxCommand getAjaxCommand(String src, Boolean cover, String loadingText) {
		return new AjaxCommand(src).setLoading(new LoadingCommand(loadingText).setCover(cover));
	}
	
	public static SubmitCommand getSubmitCommand(String src) {
		return getSubmitCommand(src, null);
	}
	
	public static SubmitCommand getSubmitCommand(String src, Boolean cover) {
		return getSubmitCommand(src, cover, null);
	}
	
	public static SubmitCommand getSubmitCommand(String src, Boolean cover, String loadingText) {
		return new SubmitCommand(src).setLoading(new LoadingCommand(loadingText).setCover(cover));
	}
	
	public static final int DIALOG_SIZE_MAX = -1;
	public static final int DIALOG_SIZE_LARGE = 0;
	public static final int DIALOG_SIZE_MEDIUM = 1;
	public static final int DIALOG_SIZE_SMALL = 2;
	
	/**
	 * 提供
	 * @param id
	 * @param title
	 * @param size 弹出框标准大小方案
	 * @param src
	 * @return
	 */
	public static DialogCommand getDialogCommand(String id, String template, String title, int size, String src) {
		template = Utils.isEmpty(template) ? "std" : template;
		String width = "";
		String height = "";
		if (size == DIALOG_SIZE_LARGE) { // 大窗口
			width = DialogCommand.WIDTH_LARGE;
			height = DialogCommand.HEIGHT_LARGE;
		} else if (size == DIALOG_SIZE_MEDIUM) { // 中窗口
			width = DialogCommand.WIDTH_MEDIUM;
			height = DialogCommand.HEIGHT_MEDIUM;
		} else if (size == DIALOG_SIZE_SMALL) { // 小窗口
			width = DialogCommand.WIDTH_SMALL;
			height = DialogCommand.HEIGHT_SMALL;
		} else {
			width = "*";
			height = "*";
		}
		int pos = DialogCommand.POSITION_MIDDLE;
		boolean cover = false;
		DialogCommand dialog = new DialogCommand(id, template, title, width, height, pos, src).setCover(cover);
		return dialog;
	}
	
	public static View buildCandidateView(List<Object[]> dataList) {
		View view = new View();
		
		VerticalLayout root = new VerticalLayout("root").setMinwidth(300).setCls("bg-white").setWmin(2).setHmin(2);
		view.add(root);
		GridPanel grid = new GridPanel("grid");
		root.add(grid, "*");
		grid.setHasTableHead(false).setNobr(true).setFace(GridPanel.FACE_NONE).setScroll(true);
		grid.setPub(new Tr().setHeight("30"));
		
		List<Object[]> gridData = new ArrayList<Object[]>();
		if (dataList != null) {
			for (Object[] item : dataList) {
				String[] data = Arrays.copyOf(item, 2, String[].class);
				if (Utils.notEmpty(data[1])) {
					data[1] = data[1].trim();
				}
				gridData.add(data);
			}
		}
		grid.setCombo(new Combo().setField(new Combo.Field("v", "n").setSearch("v")));
		grid.setLimit(10);
		
		grid.setGridData(gridData);
		grid.addColumn(GridColumn.text(1, "n", "名称", "80%").setTipfield("n").setHighlight(new Highlight(0)));
		grid.addColumn(GridColumn.text(0, "v", "值", "20%").setTipfield("v").setAlign(GridColumn.ALIGN_RIGHT).setFormat("<span style='color:green;'>$v</span>"));
		
		PageBar page = new PageBar("page", PageBar.TYPE_MINI);
		page.setTarget("grid").setAlign(PageBar.ALIGN_RIGHT);
		page.setBtncount(5);
		root.add(page, "30");
		
		return view;
	}
	
}
